package cn.edu.neusoft.cqy917.zyfypt_cqy917.model;

import java.util.List;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.bean.TwareBean;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.TwareListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.Twareiface;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.service.TwareService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class TwareModel implements Twareiface {
    private TwareService twareService;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://amicool.neusoft.edu.cn/";
    public TwareModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final TwareListener twareListener) {
        twareService = retrofit.create(TwareService.class);
        Call<List<TwareBean>> call = twareService.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<TwareBean>>() {
            @Override
            public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    twareListener.onResponse(response.body());
                else
                    twareListener.onFail("error");
            }

            @Override
            public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                twareListener.onFail("error");
            }
        });
    }
}
